import {
	defineStore
} from 'pinia';
import {
	api
} from '../script/api.js'
import {
	tools
} from '../script/tools.js'

// const emptyInfo = {
// 	islogin: false,
// 	userinfo: {},
// }

export const userinfoStore = defineStore("userinfo", {
	state: () => {
		return {
			islogin: false,
			userinfo: {},
		}
	},
	getters: {
		tbUser: (state) => {
			if (state.islogin) {
				let user = tools.concatJson(state.userinfo.tbUser)
				return user
			} else {
				return {}
			}
		},
		tbUserInfo: (state) => {
			if (state.islogin) {
				let info = tools.concatJson(state.userinfo.tbUserInfo)
				delete info.lastupdate
				delete info.email
				delete info.phone
				return info
			} else {
				return {}
			}
		}
	},
	actions: {
		/**
		 * 更新用户信息
		 */
		updateUserInfo(islogin, userinfo) {
			this.islogin = islogin
			this.userinfo = userinfo
		},
		/**
		 * 从服务器获取用户信息
		 */
		queryUserInfo(cb) {
			const instance = this
			console.log("store:", instance)
			api.get('/auth', {}, (data) => {
				console.log("服务器用户信息：", data)
				instance.updateUserInfo(data.success, data.data)
				if (cb) {
					cb(data.success)
				}
			})
		}
	}
})

export default userinfoStore